package com.hvgroup.modules.sanic.web;

import com.alibaba.fastjson2.JSONObject;
import com.hvgroup.modules.sanic.domain.dto.LlmDatasetDto;
import com.hvgroup.modules.sanic.domain.dto.LlmDatasetsTpmlDto;
import com.hvgroup.modules.sanic.domain.enums.DatasetIType;
import com.hvgroup.modules.sanic.domain.qo.CPReqBody;
import com.hvgroup.modules.sanic.service.CommDatasetService;
import com.hvgroup.utils.PageResult;
import com.hvgroup.utils.SecurityUtils;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Mono;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/dset/comm")
@Api(tags = "系统:会话管理")
public class CommDatasetController {

    private final CommDatasetService CommDatasetService;

    // 获取知识库列表  接口需要是POST以兼容不同的平台请求参数

    @PostMapping(value = "/{platformId}")
    public PageResult<LlmDatasetDto> postDataset(@PathVariable Long platformId,
                                                 @RequestBody(required = false) JSONObject body) {
        Map<String, Integer> map = (Map<String, Integer>) body.get("pageable");
        Pageable pageable = PageRequest.of(map.get("page") - 1, map.get("size"));
        CPReqBody queryData = CPReqBody.builder()
                .platformId(platformId)
                .userId(SecurityUtils.getCurrentUserId())
                .datasetIType(DatasetIType.LIST_DATASET)
                .body(body)
                .pageable(pageable)
                .build();
        return CommDatasetService.getDatasets(queryData);
    }

    // 获取知识库文档列表
    @PostMapping(value = "/{platformId}/{datasetId}")
    public PageResult<LlmDatasetsTpmlDto> postDocument(@PathVariable Long platformId,
                                                       @PathVariable String datasetId,
                                                       @RequestBody(required = false) JSONObject body) {
        Map<String, Integer> map = (Map<String, Integer>) body.get("pageable");
        Pageable pageable = PageRequest.of(map.get("page") - 1, map.get("size"));
        CPReqBody queryData = CPReqBody.builder()
                .platformId(platformId)
                .levelId_2(datasetId)
                .userId(SecurityUtils.getCurrentUserId())
                .datasetIType(DatasetIType.LIST_DOCUMENT)
                .body(body)
                .pageable(pageable)
                .build();
        return CommDatasetService.getDatasetsTpml(queryData);
    }

    // 删除知识库文档
    @PostMapping(value = "/{platformId}/{datasetId}/{documentId}")
    public Mono<Object> deleteDocument(@PathVariable Long platformId,
                                       @PathVariable String datasetId,
                                       @PathVariable String documentId,
                                       @RequestBody(required = false) JSONObject body) {
        CPReqBody queryData = CPReqBody.builder()
                .platformId(platformId)
                .levelId_2(datasetId)
                .levelId_3(documentId)
                .userId(SecurityUtils.getCurrentUserId())
                .datasetIType(DatasetIType.DELETE_DOCUMENTS)
                .body(body)
                .build();
        return CommDatasetService.deleteDocument(queryData);
    }

    //    public ResponseEntity<Object> createFile(@RequestParam String name, @RequestParam("file") MultipartFile file){
//        localStorageService.create(name, file);
//        return new ResponseEntity<>(HttpStatus.CREATED);
//    }
//    @PostMapping(value = "/upload/file/{platformId}/{datasetId}")
    @PostMapping(value = "/upload/file/{platformId}/{datasetId}")

    public Object uploadDocument(@PathVariable Long platformId,
                                       @PathVariable Long datasetId,
                                       @RequestPart("file") MultipartFile file
    ) {
        // TODO 获取到了
        CPReqBody queryData = CPReqBody.builder()
                .platformId(platformId)
                .levelId_2(String.valueOf(datasetId))
                .userId(SecurityUtils.getCurrentUserId())
                .file(file)
                .datasetIType(DatasetIType.CREATE_DOCUMENT_BY_FILE)
                .build();
        return CommDatasetService.uploadDocument(queryData);
    }

}
